// pages/admin/major/major.js
const api = require('../../../utils/api.js');
const regeneratorRuntime = require('../../../utils/runtime.js');
const config = require('../../../utils/config.js')
const util = require('../../../utils/utils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: {
      _id: '',
      name: '',
      createTime: '',
      updateTime: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let id = options.id;
    if (id !== undefined && id != "") {
      let res = await api.getDocById('mini_majors', id)
      console.info('detail -> ', res)
      this.setData({
        model: res.result.data
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 保存
   * @param {*} e 
   */
  save: async function (e) {
    wx.showLoading({
      title: '保存中...',
    })
    let that = this;
    let model = this.data.model
    model.createTime = util.tsFormatTime(Date.now())
    model.updateTime = util.tsFormatTime(Date.now())
    console.log('input model ->', model)
    if (model.name === undefined || model.name == "") {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    let result = await api.saveAdminDoc('mini_majors', model)
    if (result) {
      wx.hideLoading()
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.hideLoading()
      wx.showToast({
        title: '保存失败',
        icon: 'none',
        duration: 1500
      })
    }
  },


  onDataChange: function (e) {
    console.info('data change -> ', e)
    let model = this.data.model;
    switch (e.currentTarget.id) {
      case 'name': {
        model.name = e.detail.value
        break
      }
    }
    console.info('current change -> ', model)
    this.setData({
      model: model
    })
  },

  navBack: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },

})